package com.ice.app.system.shortcut.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ice.app.system.entity.Shortcut;
import com.ice.app.system.shortcut.service.ShortcutService;
import com.iceframework.core.base.BaseController;
import com.iceframework.core.entity.Page;
import com.iceframework.core.entity.Result;
import com.iceframework.core.utils.WebUtils;

/**
 * 
 * 控制层： 快捷方式
 * 
 * @author Mr.Palo
 * @email 442803840@qq.com
 * @date 2020-02-10 07:31:28
 */
@RestController
@RequestMapping("/sys/shortcut")
public class ShortcutController extends BaseController {

	@Autowired
	private ShortcutService shortcutService;

	/**
	 * 数据获取
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public Result getShortcut(@RequestParam(value = "key") String key, HttpServletRequest request) {
		Shortcut shortcut = shortcutService.getShortcut(key);
		return Result.success().add("shortcut", shortcut);
	}

	/**
	 * 首页加载数据
	 * 
	 * @param pn
	 * @param shortcut
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/index/data", method = RequestMethod.POST)
	@ResponseBody
	public Result shortcutIndexData(Shortcut shortcut, Page<Shortcut> page, HttpServletRequest request) {
		// 设定用户
		shortcut.setUserId(WebUtils.getUserId());
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderBy());
		List<Shortcut> shortcuts = shortcutService.getShortcuts(shortcut);
		PageInfo<Shortcut> data = new PageInfo<Shortcut>(shortcuts, page.getPageSize());
		return Result.success().add("data", data);
	}

	/**
	 * 保存实体
	 * 
	 * @param shortcut
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Result saveShortcut(Shortcut shortcut) throws Exception {
		if (shortcutService.saveShortcut(shortcut)) {
			return Result.saveSuccess();
		} else {
			return Result.saveFail();
		}
	}

	/**
	 * 删除实体
	 * 
	 * @param keys
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@ResponseBody
	public Result delShortcut(String keys) {
		if (shortcutService.deleteShortcuts(keys)) {
			return Result.delSuccess();
		} else {
			return Result.delFail();
		}
	}

}